// Help
// 
// 保佑没Bug 
//
// Created by AS on 2019/4/8. 
//
// Copyright © 2018 AS. All rights reserved.
// 

#import "OpenNumberViewController.h"
#import "MModel.h"
#import "LotteryTableViewCell.h"
@interface OpenNumberViewController ()
/**  */
@property (nonatomic,strong) NSMutableArray *dataSource;
/**  */
@property (nonatomic,strong) UIActivityIndicatorView *indicatorView;

@end

@implementation OpenNumberViewController

-(NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.view.backgroundColor = HexRGB(0xf3f5f5);
    
    self.indicatorView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    [self.indicatorView startAnimating];
    [self.tableView addSubview:self.indicatorView];
    [self.indicatorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.tableView);
        make.width.height.mas_offset(30);
        
    }];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(down_refreshData)];

    
    [self getData];
}




-(void)down_refreshData{
    [self getData];
}

-(void)getData{
    
    
    NSString *url = @"/api/lotteryOpenNumber/getDifferentLotteryCategoryNewestOpenNumber";
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    if (self.code && self.code.length > 0) {
        dict[@"parentLotteryCategoryCode"] = self.code;
    }else if (self.subCode && self.subCode.length > 0){
        url = @"/api/lotteryOpenNumber/getNewestOpenNumberByLotteryCategory";
        dict[@"lotteryCategoryCode"] = self.subCode;
        dict[@"number"] = @"100";
    }
    
    [HTTPTools POST:url parameters:dict callback:^(id result, NSString *msg, NSError *error) {
        [self.tableView.mj_header endRefreshing];
        if (!error) {
            if (!msg) {
                [self.dataSource removeAllObjects];
                [self.indicatorView stopAnimating];
                NSArray *array = [MModel mj_objectArrayWithKeyValuesArray:result];
                [self.dataSource addObjectsFromArray:array];
                [self.tableView reloadData];
            }
        }
    }];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LotteryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[LotteryTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    }
    MModel *model = self.dataSource[indexPath.row];

    if (self.subCode && self.subCode.length > 0) {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell setModel:model withHideIcon:YES];
    }else{
        [cell setModel:model withHideIcon:NO];
    }
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (self.subCode) {
        return;
    }
    MModel *model = self.dataSource[indexPath.row];
    OpenNumberViewController *openNumberView = [OpenNumberViewController new];
    openNumberView.navigationItem.title = model.name;
    openNumberView.subCode = model.subCode;
    [self pushViewController:openNumberView];
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    MModel *model = self.dataSource[indexPath.row];
    if ([model.code isEqualToString:@"lhc"]) {
        return ZoomHeight(80);
    }
    return ZoomHeight(70);
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.00001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 1;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}


-(void)dealloc{
    NSLog(@"== dealloc ==");
    if (self.deallocSignalBlock) {
        self.deallocSignalBlock();
    }
}

@end
